package org.example.springbirthday.service;

import lombok.extern.slf4j.Slf4j;
import org.example.springbirthday.mapper.BlessInfoMapper;
import org.example.springbirthday.mapper.UserInfoMapper;
import org.example.springbirthday.model.BlessInfo;
import org.example.springbirthday.model.UserInfo;
import org.example.springbirthday.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 九转苍翎
 * Date: 2025/8/7
 * Time: 15:40
 */
@Service
@Slf4j
@RequestMapping("/bless")
public class BlessService {

    @Autowired
    private BlessInfoMapper blessInfoMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;

    public List<BlessInfo> getBlessInfoListByPageAndSize(Integer offset, Integer size) {
        return blessInfoMapper.getBlessInfoListByPageAndSize(offset,size);
    }

    public Integer addBlessInfo(BlessInfo blessInfo,Integer userId) {
        UserInfo userInfo = userInfoMapper.getUserById(userId);
        log.info("发布祝福语,userInfo:{}",userInfo);
        userInfo.setBlessCount(userInfo.getBlessCount() + 1);
        //修改用户发布的祝福数
        Integer addBlessCount = userInfoMapper.updateBlessCount(userInfo);
        //修改祝福语总数
        Integer count = blessInfoMapper.count();
        Integer ret = blessInfoMapper.updateCount(++count);
        //设置祝福语的相关信息
        blessInfo.setUserId(userInfo.getId());
        blessInfo.setUserName(userInfo.getUserName());
        blessInfo.setCreateTimeStatus(DateUtils.dateFormat(new Date()));
        blessInfo.setUpdateTimeStatus(DateUtils.dateFormat(new Date()));
        log.info("发布祝福语,blessInfo:{}",blessInfo);
        return blessInfoMapper.addBlessInfo(blessInfo) + addBlessCount + ret;
    }

    public List<BlessInfo> getBlessInfoListByUserId(Integer userId) {
        return blessInfoMapper.getBlessInfoListByUserId(userId);
    }

    public Integer deleteBlessInfo(Integer id,Integer userId) {
        UserInfo userInfo = userInfoMapper.getUserById(userId);
        userInfo.setBlessCount(userInfo.getBlessCount() - 1);
        //修改用户发布的祝福数
        Integer ret1 = userInfoMapper.updateBlessCount(userInfo);
        //修改祝福语总数
        Integer count = blessInfoMapper.count();
        Integer ret2 = blessInfoMapper.updateCount(--count);
        return blessInfoMapper.deleteBlessInfo(id) + ret1 + ret2;
    }

    public Integer updateBlessInfo(BlessInfo blessInfo) {
        blessInfo.setUpdateTimeStatus(DateUtils.dateFormat(new Date()));
        return blessInfoMapper.updateBlessInfo(blessInfo);
    }
}
